#coding=utf-8
from selenium import webdriver
import time
import re
from bs4 import BeautifulSoup
import sys
from selenium.webdriver.common.action_chains import ActionChains
import requests



def find_all_name():
    print('hello!!!')

opt = webdriver.ChromeOptions()                 #创建浏览器
# opt.set_headless()                            #无窗口模式
m_driver = webdriver.Chrome(options=opt)          #创建浏览器对象
m_driver.get('http://i.chaoxing.com/base')   #打开网页
m_driver.find_element_by_xpath("//*/ul/li[text()='手机号/超星号登录']").click()
m_driver.find_element_by_xpath("//input[@placeholder='请输入手机号/超星号']").send_keys('17349860282')
m_driver.find_element_by_xpath("//input[@placeholder='请输入密码']").send_keys('wanghao100')
m_driver.find_element_by_xpath("//input[@value='登录']").click()
time.sleep(1)                                   #加载等待
#m_driver.find_element_by_xpath("//a[@name='课程']").click()
#time.sleep(3)     
m_driver.switch_to.default_content() 
frame = m_driver.find_elements_by_tag_name('iframe')[0]  
m_driver.switch_to.frame(frame)      
m_driver.find_element_by_link_text("大学语文").click()
#m_driver.find_element_by_xpath("//a[@href='https://mooc1-1.chaoxing.com/mycourse/studentcourse?courseId=201624406&vc=1&clazzid=12795689&enc=63464d4de3df068472c9949cd22f1d16']").click()
#m_driver.find_elements_by_class_name('clearfix')[1].click()

time.sleep(1)
m_driver.switch_to.window(m_driver.window_handles[1])
time.sleep(1)
#m_driver.find_element_by_link_text("1.1").click()
#m_driver.find_element_by_xpath("//a[@title='孔子其人——家世、品行与流离']").click()
m_driver.find_elements_by_class_name("articlename")[0].click()
time.sleep(1)
driver1=m_driver
need_to_do=driver1.find_elements_by_class_name("ncells")
#print('有几个呢'+str(len(need_to_do)))


#print(type(m_driver.page_source))
#need_to_do[1].click()
for i in range(len(need_to_do)):
    m_driver.switch_to.default_content()
    driver1.switch_to.default_content()
    need_to_do=driver1.find_elements_by_class_name("ncells")
    
    # try:
    div_bf=BeautifulSoup(driver1.page_source,"html.parser")
    result=div_bf.find_all('div',class_="ncells")   
    
    div_bf=BeautifulSoup(str(result[i]),"html.parser")
    #print(div_bf.span)
    result=div_bf.find_all('input')
    #print(result)
    if len(result)==0:
        print('第'+str(i)+'小结已经完成，不需要刷，即将载入下一节')
    else :
        print('第'+str(i)+'小结未完成')
        print('开始刷课')
        time.sleep(2)
        need_to_do[i].click()
        print(i)
        time.sleep(2)
        iframe = m_driver.find_element_by_css_selector('iframe')
        m_driver.switch_to.frame(iframe)
        iframe = m_driver.find_element_by_css_selector('iframe')
        m_driver.switch_to.frame(iframe)
        mouse=m_driver.find_element_by_xpath("//div[@aria-label='视频播放器']")
        ActionChains(m_driver).move_to_element(mouse).perform()
        mouse.click()
        
        time.sleep(1)
        div_bf=BeautifulSoup(m_driver.page_source,"html.parser")
        result=div_bf.find_all('span',class_="vjs-duration-display")   
        full_time=result[0].text
        now_time=''
        last_time=''
        while full_time!=now_time:
            div_bf=BeautifulSoup(m_driver.page_source,"html.parser")
            result=div_bf.find_all('span',class_="vjs-current-time-display")   
            now_time=result[0].text
            if last_time==now_time:
                #mouse.click()
                ans=input('因为某种原因暂停\n1若是题选1，\n否则enter就好')
                if ans=='1':

                    choice=m_driver.find_elements_by_xpath("//input[@name='ans-videoquiz-opt']")
                    print'共有'+(len(choice)+'choice')
                    for j in choice:
                        j.click()

                        choice=m_driver.find_element_by_xpath("//div[@class='ans-videoquiz-submit']").click()
                        try:
                            
                        time.sleep(2)
                        
                else:
                    m_driver.find_element_by_xpath("//button[@title='播放']").click()
                    print('重新点击')
                # try:
                   
                # except:

            last_time=now_time
            sys.stdout.write(now_time+'/'+full_time+'\r')
            sys.stdout.flush()
            ActionChains(m_driver).move_to_element(mouse).perform()
            #print(now_time+'/'+full_time)
            time.sleep(2)
        print('第'+str(i)+'小结完成')    
    #status=result[0].text
    






    # except:
    #     print('未完成')
    #     need_to_do[i].click()
    #     print(i)
    #     time.sleep(1)
    #     iframe = m_driver.find_element_by_css_selector('iframe')
    #     m_driver.switch_to.frame(iframe)
    #     iframe = m_driver.find_element_by_css_selector('iframe')
    #     m_driver.switch_to.frame(iframe)
    #     m_driver.find_element_by_xpath("//div[@aria-label='视频播放器']").click()
    #     time.sleep(1)


    #     div_bf=BeautifulSoup(m_driver.page_source,"html.parser")
    #     result=div_bf.find_all('span',class_="vjs-duration-display")   
    #     full_time=result[0].text
    #     now_time=''
    #     while full_time!=now_time:
    #         div_bf=BeautifulSoup(m_driver.page_source,"html.parser")
    #         result=div_bf.find_all('span',class_="vjs-current-time-display")   
    #         now_time=result[0].text
    #         print(now_time)
    #         time.sleep(1)
print('快看看怎么样！哈哈哈哈')




#m_driver.maximize_window()                      #最大化窗口
#time.sleep(2)                                   #加载等待
#m_driver.find_element_by_css_selector('手机号/超星号登录').click()
#m_driver.find_element_by_xpath("./*//span[@class='bg s_ipt_wr quickdelete-wrap']/input").send_keys("魅族")    #利用xpath查找元素进行输入文本
# m_driver.find_element_by_id('unameId').send_keys("17349860282") #候选方法
# m_driver.find_element_by_id('passwordId').send_keys("wanghao100") #候选方法

#m_driver.find_element_by_id('passwordId'). #候选方法
#m_driver.find_element_by_xpath("//span[@class='bg s_btn_wr']/input").click()#点击按钮
#m_driver.find_element_by_link_text("电力系统分析").click()#候选方法
# m_driver.find_element_by_id('1').click()#候选方法

#m_driver.close()
# m_driver.find_element_by_xpath("//span[@class='bg s_btn_wr']/input[type='submit'][value='百度一下']").click()#候选方法,多条件匹配
